﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WCYCReader.Main;
using WCYCReader.Utility;
using Microsoft.Phone.Info;
using WCYCReader.Main.Core;
using System.Windows.Input;
using Microsoft.Phone.Tasks;

namespace WCYCReader.Pages
{
    public partial class FeedbackPage : PhoneApplicationPage
    {
        public FeedbackPage()
        {
            this.InitializeComponent();
            App.QQReader_prePageId = App.QQReader_pageId;
            App.QQReader_pageId = "QQReader_FeedbackPage";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Feedback_Submit();
        }

        private void Feedback_DownloadStringCompleted(object sender, WCYCReader.Main.Core.DownloadStringCompletedEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (e.Result == null)
                {
                    Prompt.PromptMessage("提示：网络异常！");
                    return;
                }
                Prompt.PromptMessage("提示：反馈完成。");
            });
        }

        private void Feedback_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).Text = "";
        }

        private void Feedback_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                base.Focus();
                this.Feedback_Submit();
            }
        }

        private void Feedback_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox.Text == "")
            {
                textBox.Text = "添加您的反馈意见吧（最多200字）";
            }
        }

        private void Feedback_Submit()
        {
            if (this.Feedback.Text == "添加您的反馈意见吧（最多200字）" || string.IsNullOrWhiteSpace(this.Feedback.Text))
            {
                Prompt.PromptMessage("提示：请填写反馈信息。");
            }
            else if (this.Feedback.Text.Length <= 200)
            {
                //Dictionary<string, string> strs = new Dictionary<string, string>()
                //{
                //    { "qqNo", (this.qqNumber.Text == "QQ号码" ? "0" : this.qqNumber.Text) },
                //    { "mobileNo", (this.phoneNumber.Text == "电话号码" ? "0" : this.phoneNumber.Text) },
                //    { "content", (this.Feedback.Text == "添加您的反馈意见吧（最多200字）" ? "0" : this.Feedback.Text) }
                //};
                //WebHeaderCollection webHeaderCollections = new WebHeaderCollection();
                //if (SettingHelper.IsSettings("QQReader_sid"))
                //{
                //    webHeaderCollections["sid"] = SettingHelper.GetValueFromSettings("QQReader_sid") as string;
                //}
                //webHeaderCollections["c_version"] = "qqreader_1.0.0_0001_wp7";
                //webHeaderCollections["ua"] = DeviceExtendedProperties.GetValue("DeviceName").ToString();
                //webHeaderCollections["c_platform"] = "wp7";
                //HttpPostClient httpPostClient = new HttpPostClient(strs, webHeaderCollections);
                //httpPostClient.DownloadStringAsync(new Uri("http://allreader.3g.qq.com/feedback", UriKind.Absolute));
                //httpPostClient.DownloadStringCompleted += new HttpPostClient.DownloadStringCompletedHandler(this.Feedback_DownloadStringCompleted);
                EmailComposeTask emailComposeTask = new EmailComposeTask();
                emailComposeTask.Subject = "我有些意见";
                emailComposeTask.Body = string.Concat(this.Feedback.Text.Trim(), Environment.NewLine, this.qqNumber.Text.Trim(), Environment.NewLine, this.phoneNumber.Text);
                emailComposeTask.To = "3122242921@qq.com";

                emailComposeTask.Show();
            }
            else
            {
                Prompt.PromptMessage("提示：反馈信息超过200字。");
            }
            this.qqNumber.Text = "QQ号码";
            this.phoneNumber.Text = "电话号码";
            this.Feedback.Text = "添加您的反馈意见吧（最多200字）";

            
        }

        private void Feedback_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.Feedback.Text.Length > 200)
            {
                Prompt.PromptMessage("提示：您的反馈字数已超过200字。");
            }
        }

        private void phoneNumber_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).Text = "";
        }

        private void phoneNumber_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox.Text == "")
            {
                textBox.Text = "电话号码";
            }
        }

        private void qqNumber_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).Text = "";
        }

        private void qqNumber_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox.Text == "")
            {
                textBox.Text = "QQ号码";
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.Feedback_Submit();
        }
    }
}